package com.jessechanrui.template.database.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jessechanrui.template.database.mybatisplus.assembler.UserAssembler;
import com.jessechanrui.template.database.mybatisplus.dto.ResponseDTO;
import com.jessechanrui.template.database.mybatisplus.dto.UserDTO;
import com.jessechanrui.template.database.mybatisplus.entity.UserDO;
import com.jessechanrui.template.database.mybatisplus.entity.vo.Test2VO;
import com.jessechanrui.template.database.mybatisplus.entity.vo.Test3VO;
import com.jessechanrui.template.database.mybatisplus.entity.vo.TestVO;
import com.jessechanrui.template.database.mybatisplus.service.IUserService;
import com.jessechanrui.template.database.mybatisplus.service.event.MsgEventPubLisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description： 用户controller
 * @author：weirx
 * @date：2022/1/17 17:39
 * @version：3.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private MsgEventPubLisher msgEventPubLisher;

    /**
     * description: 新增

     * @return: boolean
     * @author: weirx
     * @time: 2022/1/17 19:11
     */
    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity save(@RequestBody UserDTO user) {
        UserDO userDO = UserAssembler.INSTANCE.toDO(user);
        Test2VO test2VO = new Test2VO();
        test2VO.setName1("name1");
        test2VO.setName2("name2");

        Test3VO test3VO = new Test3VO();
        test3VO.setName1("name1");
        test3VO.setName3("name2");

        TestVO testVO = new TestVO();
        testVO.setName("name");
        testVO.setTest(test2VO);
        userDO.setTest(testVO);



        UserDO userDO2 = UserAssembler.INSTANCE.toDO(user);
        userDO2.setTest(testVO);
        TestVO testVO2 = new TestVO();
        testVO2.setName("name");
        testVO2.setTest(test2VO);
        userDO2.setTest(testVO2);

        msgEventPubLisher.pushListener("创建用户：" + userDO.getNickname());
        userService.save(userDO2);
        return ResponseEntity.status(HttpStatus.OK).body(new ResponseDTO(200, userService.save(userDO)));
    }

    /**
     * description: 修改
     * @param nickname
     * @param id
     * @return: boolean
     * @author: weirx
     * @time: 2022/1/17 19:11
     */
    @RequestMapping("/update")
    public boolean update(@RequestParam String nickname, @RequestParam Long id) {
        UserDO userDO = new UserDO();
        userDO.setNickname(nickname);
        userDO.setId(id);
        return userService.updateById(userDO);
    }

    /**
     * description: 删除
     * @param id
     * @return: boolean
     * @author: weirx
     * @time: 2022/1/17 19:11
     */
    @RequestMapping("/delete")
    public boolean delete(@RequestParam Long id) {
        UserDO userDO = new UserDO();
        userDO.setId(id);
        return userService.removeById(userDO);
    }

    /**
     * description: 列表
     * @return: java.util.List<com.wjbgn.user.entity.UserDO>
     * @author: weirx
     * @time: 2022/1/17 19:11
     */
    @RequestMapping("/list")
    public List<UserDO> list() {
        return userService.list();
    }

    /**
     * description: 分页列表
     * @param current
     * @param size
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page
     * @author: weirx
     * @time: 2022/1/17 19:11
     */
    @RequestMapping("/page")
    public Page page(@RequestParam int current, @RequestParam int size) {
        return userService.page(new Page<>(current,size), new QueryWrapper(new UserDO()));
    }

}